<?php

class Model_urls extends MY_Model
{
    protected $_table = 'urls';
    protected $_primaryKey = 'id';

    public function __construct()
    {
        parent::__construct();
        $this->db = $this->load->database('default', true);
        $this->setDb($this->db);
    }

    /**
     * 分页查询列表
     * @param int $page 当前页码 =false时不分页
     * @param array $params 查询条件组合
     * @param int $pageSize 每页数量
     * @param array $order  排序值
     * @return type
     */
    public function getList($page, $params = array(), $pageSize = 10, $fields = 'id,title,url,IF(icon_sina="", icon, icon_sina) icon,remark,status,`show`', $order = ['weight', 'DESC'])
    {
        $this->db->select($fields);
        $this->db->from($this->getTable());
        $this->makeCond('', $params);
        $this->db->order_by($order[0], $order[1]);
        if ($page) {
            $this->db->limit($pageSize, ($page - 1) * $pageSize);
        }
        return $this->db->get()->result_array();
    }

    /**
     * 根据sql语句查询结果集
     * @param string $sql
     * @param array $params
     * @return array
     */
    public function query($sql, $params = array())
    {
        $query = null;
        if (empty($params)) {
            $query = $this->db->query($sql);
        } else {
            $query = $this->db->query($sql, $params);
        }
        return empty($query) ? array() : $query->result_array();
    }

    /**
     * 根据sql语句执行处理
     * @param string $sql
     * @param array $params
     * @return array
     */
    public function queryForUpdate($sql, $params = array())
    {
        $query = null;
        if (empty($params)) {
            $query = $this->db->query($sql);
        } else {
            $query = $this->db->query($sql, $params);
        }
        return $this->db->affected_rows();
    }

}
